// 封装各种接口
import request from './request.js'
// import { getToken } from '@/utils/token.js
// 刷新Token
export const refreshTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  header: {
    Authorization: 'Bearer' + localStorage.getItem('refreshToken')
  }
})
// 获取文章列表
// 文章 - 获取列表
export const articlesListAPI = ({
  channelId,
  timestamp
}) => request({
  url: '/v1_0/articles',
  params: {
    channel_id: channelId,
    timestamp: timestamp || (new Date()).getTime()
  }
})
// 获取所有频道列表
export const allChannelAPI = () => request({
  url: '/v1_0/channels',
  method: 'GET'
})

// 获取用户选择的频道
export const userChannelAPI = () => request({
  url: '/v1_0/user/channels',
  methods: 'GET'
})

// 频道-更新频道
export const updataChannelAPI = (channels) => request({
  url: '/v1_0/user/channels',
  methods: 'PUT',
  data: {
    channels
  }
})

// 文章-反馈-不感兴趣
export const dislikeArticleAPI = ({ artId }) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  // headers: { Authorization: `Bearer ${getToken()}` },
  data: { target: artId }
})

// 文章-反馈-举报
export const reportArticleAPI = ({ artId, artType }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  // headers: { Authorization: `Bearer ${getToken()}` },
  data: {
    target: artId,
    type: artType,
    remark: ''
  }
})
// 搜索-联想
export const suggestAPI = ({ query }) => request({
  url: '/v1_0/suggestion',
  method: 'GET',
  params: {
    q: query
  }
})
// 搜索-搜索结果列表
export const searchResultsAPI = ({ query }) => request({
  url: '/v1_0/search',
  method: 'GET',
  params: {
    page: 1,
    per_page: 10,
    q: query
  }

})
// 文章-获取文章详情
export const articleDetailAPI = ({ id }) => request({
  url: `/v1_0/articles/${id}`,
  method: 'GET',
  params: {
    article_id: id
  }

})
// 文章-关注作者
export const followAuthorAPI = ({ id }) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: id
  }

})
// 文章-取关作者
export const unfollowAuthorAPI = ({ id }) => request({
  url: `/v1_0/user/followings/${id}`,
  method: 'DELETE'
})
// 文章-点赞
export const likingAPI = ({ id }) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: id
  }
})
// 文章-取消点赞
export const dislikingAPI = ({ id }) => request({
  url: `/v1_0/article/likings/${id}`,
  method: 'DELETE'
})
// 文章-评论列表
export const commentAPI = ({ id }) => request({
  url: '/v1_0/comments',
  method: 'GET',
  params: {
    type: 'a',
    source: id,
    offset: null,
    limit: 1000
  }
})
// 评论-喜欢
export const likeCommentAPI = ({ comId }) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: comId
  }
})
// 评论-不喜欢
export const dislikeCommentAPI = ({ comId }) => request({
  url: `/v1_0/article/likings/${comId}`,
  method: 'DELETE'
})
// 评论-增加评论
export const addCommentAPI = ({ comId, content, artId }) => request({
  url: '/v1_0/comments',
  method: 'POST',
  data: {
    target: comId,
    content: content,
    art_id: artId
  }
})
// 用户-获取用户基本资料
export const userAPI = () => request({
  url: '/v1_0/user',
  method: 'GET'

})
// 用户-个人简介
export const userInfoAPI = () => request({
  url: '/v1_0/user/profile',
  method: 'GET'
})
// 用户-更新头像
export const updatePhotoAPI = ({ fd }) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  // 注意：这里body参数格式为multipart/form-data
  data: fd// fd对应传过来的FormData()表单对象
})
// 用户-更新用户资料

export const updateInfoAPI = (dataObj) => {
  // 有值才带参数名给后台，无值参数名不携带
  const obj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const i in obj) {
    if (dataObj[i] === undefined) { // 没找到证明没传，移除属性和值
      delete obj[i]
    } else {
      obj[i] = dataObj[i]
    }
  }
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    // data不会忽略值为null的那对key+value,params遇到null值会忽略
    data: obj
  })
}
